<template>
  <page-wrap
    ref="pageWrap"
    hideTab
    back
    :title="title"
    :dark="loaded ? false : true"
    :hideSafeArea="true"
    bgColor="#F7F8FA"
  >
    <view class="past-activity" :style="'padding-top:' + navHeight + 'px;'">
      <scroll-view
        class="activity-list"
        scroll-y="true"
        @scrolltolower="reachBottom"
      >
        <ActivityCard
          v-for="(item, index) in pastActivityList"
          :key="index"
          :imageSrc="item.atPic"
          :activityTitle="item.atName"
          :time="changeDate(item.atStartTime)"
          :position="item.address"
          :price="item.price"
          :isPassActivity="true"
          :termName="item.termName"
          :officialAccountLink="item.officialAccountLink"
          @activityHandleClick="activityHandleClick(item)"
        />
        <Empty
          v-if="hotActivityList.length < 1"
          :baseImg="emptyImg"
          text="暂无数据"
          :fontWeight="400"
          textColor="#bfbfbf"
          class="empty"
          spacing="0"
        />
      </scroll-view>
    </view>
  </page-wrap>
</template>

<script>
import Empty from "@/components/Empty.vue";

import ActivityCard from "../components/activityCard.vue";
import { getActivityTermList } from "@/api/modules/zty.js";

export default {
  components: {
    Empty,
    ActivityCard,
  },
  data() {
    return {
      emptyImg: this.$imgBaseSelf + "common/empty.png",

      date: new Date().toISOString(), //现在时间
      title: "活动回顾",
      navHeight: getApp().globalData.navHeight,
      pastActivityList: [],
      pageNo: 1,
    };
  },
  methods: {
    activityHandleClick(item) {
      console.log("aaaaaaa", item);
      let personId = uni.getStorageSync("person-id").slice(2) || "";
      if (!personId) {
        // 在这个地方，登录成功后会在loginSuccessBack这回调里面请求用户数据
        this.showLoginDialog();
      } else {
        if (item.officialAccountLink) {
          uni.navigateTo({
            url:
              "/pagesSub/careerGrow/careerSearch/webview?url=" +
              item.officialAccountLink,
          });
        } else {
          return;
        }
      }
    },
    reachBottom() {
      console.log("这里触底了");
      this.pageNo++;
      this.getActivityTermListFn("30");
    },
    changeDate(date) {
      let myDay = new Date(date.slice(0, 10)).getDay(); //将返回0，表示星期天
      switch (myDay) {
        case 0:
          myDay = date.slice(0, 10) + " 周日 " + date.slice(11, 16);
          break;
        case 1:
          myDay = date.slice(0, 10) + " 周一 " + date.slice(11, 16);
          break;
        case 2:
          myDay = date.slice(0, 10) + " 周二 " + date.slice(11, 16);
          break;
        case 3:
          myDay = date.slice(0, 10) + " 周三 " + date.slice(11, 16);
          break;
        case 4:
          myDay = date.slice(0, 10) + " 周四 " + date.slice(11, 16);
          break;
        case 5:
          myDay = date.slice(0, 10) + " 周五 " + date.slice(11, 16);
          break;
        case 6:
          myDay = date.slice(0, 10) + " 周六 " + date.slice(11, 16);
          break;
        default:
          break;
      }
      return myDay;
    },
    dateFormat(time) {
      let date = new Date(time);
      let year = date.getFullYear();
      // 在日期格式中，月份是从0开始的，因此要加0，使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
      let month =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1;
      let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      let hours =
        date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
      let minutes =
        date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      let seconds =
        date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      // 拼接
      return (
        year +
        "-" +
        month +
        "-" +
        day +
        " " +
        hours +
        ":" +
        minutes +
        ":" +
        seconds
      );
      // return year + "-" + month + "-" + day;
    },
    async getActivityTermListFn(searchType) {
      const params = {
        searchType: searchType,
        currentTime: this.dateFormat(this.date),
        isRecommend: searchType == "10" ? "1" : "2",
        pageSize: 10,
        pageNo: this.pageNo,
      };
      const res = await getActivityTermList(params);
      if (res.code == 200) {
        if (searchType == "30") {
          // this.pastActivityList = res.data;
          this.pastActivityList.push.apply(this.pastActivityList, res.data);
          // 往期回顾
        } else {
          // 热门活动
          this.currentActivityList = res.data;
        }
      }
    },
  },
  created() {
    this.getActivityTermListFn("30");
  },
};
</script>

<style lang="scss" scoped>
.past-activity {
  width: 100%;
  height: 100%;
  .activity-list {
    background-color: #ffffff;
    border-radius: 24rpx;
    height: calc(100vh - 100px);
    background: #ffffff;
    padding: 32rpx;
    box-sizing: border-box;
  }
}
</style>
